package com.yiqi.liebang.feature.news.view;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.widget.TabEntity;
import com.yiqi.liebang.entity.bo.ReadButtomBo;
import com.yiqi.liebang.feature.login.view.LoginActivity;
import com.yiqi.liebang.feature.order.view.OrderQuestionFragment;
import com.yiqi.liebang.feature.people.view.FriendsListActivity;
import com.yiqi.liebang.feature.people.view.PeopleActivity;
import com.yiqi.liebang.framework.api.HomeApi;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;


public class NewsFragment extends BaseFragment {

    @BindView(R.id.tvviewpager)
    ViewPager mViewPager;
    @BindView(R.id.btn_to_list)
    ImageView mBtnToList;
    @BindView(R.id.tb_msg)
    CommonTabLayout mTbMsg;

    private int mPos = 0;
    private final String[] mTitles = {
            "私信", "访客", "系统"
    };

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    ArrayList<Fragment> mFragments = new ArrayList<>();
    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_news;
    }

    @Override
    protected void initInjector() {

    }


    @Override
    protected void initView() {
        initViewPager();
    }

    @Override
    protected void initData() {
        if (!AM.user().isLogin()) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
        }else {
            getUnReadTips();
        }
    }

    @Override
    protected void initEvent() {

    }

    private void initViewPager() {
        // 创建一个集合,装填Fragment

        // 装填
        mFragments=new ArrayList<>();
        mFragments.add(new MsgFragment());
        mFragments.add(new VisitorsFragment());
        mFragments.add(new SysPushFragment());



        MyPagerAdapter  mAdapter = new MyPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mTabEntities=new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], R.drawable.white_radius, R.drawable.white_radius));
        }
        mTbMsg.setTabData(mTabEntities);
        mTbMsg.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTbMsg.setCurrentTab(position);
                mPos = position;
                switch (position) {
                    case 0:
                        mBtnToList.setVisibility(View.VISIBLE);
                        mBtnToList.setImageResource(R.drawable.nav_button_addressbook);
                        break;
                    case 1:
                        mBtnToList.setVisibility(View.VISIBLE);
                        mBtnToList.setImageResource(R.drawable.nav_button_jiahaoyou);
                        break;
                    case 2:
                        mBtnToList.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    @OnClick(R.id.btn_to_list)
    public void onViewClicked() {
        if (!AM.user().isLogin()) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
            return;
        }
        switch (mPos) {
            case 0:
                startActivity(new Intent(getActivity(), FriendsListActivity.class));
                break;
            case 1:
                startActivity(new Intent(getActivity(), PeopleActivity.class));
                break;

        }
    }


    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {

        }
    }


    private void getUnReadTips() {
        AM.api().createApiService(HomeApi.class)
                .getRedButton()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<ReadButtomBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(ReadButtomBo readButtomBo) {
                        if (readButtomBo.getPrivateLetter()) {
                            mTbMsg.showDot(0);
                        }
                        if (readButtomBo.getVisit()) {
                            mTbMsg.showDot(1);
                        }
                        if (readButtomBo.getSystem()){
                            mTbMsg.showDot(2);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
